#ifndef __PERSISTENCE_H__
#define __PERSISTENCE_H__

#include "CCUserDefault.h"
USING_NS_CC;

#define MAX_LEVEL 14
#define KEY_MAX_PASSED_LEVEL "key-yanni-suta-max-passed-level"
#define KEY_LEVEL_STAR_CNT "key-yanni-suta-level-star-cnt"
#define KAY_EFFECT_ENABLED "key-yanni-suta-effect-enabled"

namespace suta
{
	class Persistence
	{
		Persistence(){};
		~Persistence(){};
	public:
		static Persistence*shardPersiatence()
		{
			static Persistence*singleton=NULL;
			if (!singleton)
			{
				singleton=new Persistence();
			}
			return singleton;
		};

		int getMaxPassedLevel()
		{
			CCUserDefault*user=CCUserDefault::sharedUserDefault();
			int maxPassedLevel=user->getIntegerForKey(KEY_MAX_PASSED_LEVEL,-1);
			if (maxPassedLevel>MAX_LEVEL)
			{
				maxPassedLevel=MAX_LEVEL;
			} 
			else if(maxPassedLevel<-1)
			{
				maxPassedLevel=-1;
			}
			return maxPassedLevel;
		};

		void setMaxPassedLevel(int level)
		{
			if (level<0||level>MAX_LEVEL)
			{
				CCLog("Illegal parameters");
				return;
			} 

			CCUserDefault*user=CCUserDefault::sharedUserDefault();
			if (getMaxPassedLevel()>level)
			{
				return;
			}
			user->setIntegerForKey(KEY_MAX_PASSED_LEVEL,level);
			user->flush();
		};

		void setLevelStarCnt(int level,int starCnt)
		{
			if (level<0||level>MAX_LEVEL||starCnt<0||starCnt>3)
			{
				CCLog("Illegal parameters");
				return ;
			}
			CCUserDefault*user=CCUserDefault::sharedUserDefault();
			char str[50]={0};
			SPRINTF(str,50,"%s-%d",KEY_LEVEL_STAR_CNT,level);
			
			if (getLevelStarCnt(level)>starCnt)
			{
				return;
			}

			user->setIntegerForKey(str,starCnt);
			user->flush();
		};

		int getLevelStarCnt(int level)
		{
			if (level<0||level>MAX_LEVEL)
			{
				return 0;
			}
			CCUserDefault*user=CCUserDefault::sharedUserDefault();
			char str[50]={0};
			SPRINTF(str,50,"%s-%d",KEY_LEVEL_STAR_CNT,level);
			int stars=user->getIntegerForKey(str,0);
			if (stars<0)
			{
				stars=0;
			} 
			else if(stars>3)
			{
				stars=3;
			}

			return stars;
		};

		inline void setEffectEnabled(bool enabled)
		{
			CCUserDefault*user=CCUserDefault::sharedUserDefault();
			user->setBoolForKey(KAY_EFFECT_ENABLED,enabled);
			user->flush();
		};
		inline bool getEffectEnabled()
		{
			CCUserDefault*user=CCUserDefault::sharedUserDefault();
			return user->getBoolForKey(KAY_EFFECT_ENABLED,true);
		};

	};
}//namespace suta

#endif//__PERSISTENCE_H__